<?php 
require_once('simpletest/autorun.php');
require_once('../src/scottlib.php');
require_once('../src/scottlib-orm.php');


/**
 * Tests for the Criteria classes in the ORM module.
 * @author Aaron Collegeman aaron@collegeman.net
 * @since 1.0.3-BETA
 */
class TestCriteria extends UnitTestCase {
	
	function setUp() {
		$this->conn = connect(':/tmp/mysql.sock', 'root', 'password', 'mysql');
		//$this->conn = connect('localhost', 'root', '14allall41', 'mysql');
	}
	
	function tearDown() {
		disconnect();
	}
	
	function testEq() {
		eval(Criteria::import());
		
		$criteria = $eq('foo', 'bar'); 
		
		$this->assertEqual("`foo` = 'bar'", $criteria->build(new ObjectClass()));
	}
	
	function testNe() {
		eval(Criteria::import());
		
		$criteria = $ne('foo', 'bar'); 
		
		$this->assertEqual("`foo` <> 'bar'", $criteria->build(new ObjectClass()));
	}
	
	
	function testAnd() {
		eval(Criteria::import());
		
		$criteria = $and(
			$eq('foo', 'bar'),
			$ne('goose', 'cooked')
		);
		
		$this->assertEqual(" ( `foo` = 'bar' AND `goose` <> 'cooked' ) ", $criteria->build(new ObjectClass()));
	}
	
	function testOr() {
		eval(Criteria::import());
		
		$criteria = $or(
			$eq('foo', 'bar'),
			$ne('goose', 'cooked')
		);
		
		$this->assertEqual(" ( `foo` = 'bar' OR `goose` <> 'cooked' ) ", $criteria->build(new ObjectClass()));
	}
	
	function testEach() {
		eval(Criteria::import());
		
		$criteria = $or(
			$each(array('one', 'two'), 'value', $and( 
				$eq('foo', ':value'),
				$ne('bar', ':value')	
			))
		);
		
		$this->assertEqual(" (  ( `foo` = 'one' AND `bar` <> 'one' )  OR  ( `foo` = 'two' AND `bar` <> 'two' )  ) ", $criteria->build(new ObjectClass()));
		
		$criteria = $and(
			$each(array('one', 'two'), 'value', $or( 
				$eq('foo', ':value'),
				$ne('bar', ':value')	
			))
		);
		
		$this->assertEqual(" (  ( `foo` = 'one' OR `bar` <> 'one' )  AND  ( `foo` = 'two' OR `bar` <> 'two' )  ) ", $criteria->build(new ObjectClass()));
		
		$criteria = $or(
			$each(array('one', 'two'), array('value1', 'value2'), $and( 
				$eq('foo', ':value1'),
				$ne('bar', ':value2')	
			))
		);
		
		$this->assertEqual(" (  ( `foo` = 'one' AND `bar` <> 'one' )  OR  ( `foo` = 'two' AND `bar` <> 'two' )  ) ", $criteria->build(new ObjectClass()));
	}
	
}

class ObjectClass extends slObject {

	private $_name;
	
	public $foo;
	
	public $sample;
	
	function getName() {
		return $this->_name;
	}	
	
	function setName($name) {
		$this->_name = $name;
	}
}


